import re;
from ngram import En_NgramBuilder as NgramBuilder;

from category.CategoryGrabber import CategoryGrabber as CategoryGrabber;
from category.MarkovBlanketGrabber import BlanketRetriever as BlanketRetriever;
from util.En_Formatter import En_Formatter as Formatter;

from corpus import PageFormer as pf;
from corpus.BlanketCorpusBuilder import BlanketCorpusBuilder as CorpusBuilder;
from corpus.BlanketSubjectsGrabber import BlanketSubjectsGrabber as SubjectsGrabber;




def printMenu():
        print(''' 
    --- Wikipedia Data Thingy ---
          *** Main Menu ***  
          1: Run The Program...
          2: Exit The Program...
    ''');



cat = "";
text = "";
choice = 0;
wikiGrab = CategoryGrabber()


while not(choice == "2"):
    printMenu();
    choice = raw_input("Choice: ");
    
    if(choice == "1"):
        #Then we run the program... DUH!!!
        text = "";
        cat = Formatter.dealWithSpaces(raw_input("Category: "));
        print("Building Category Blanket...");
        (blanket, sub) = BlanketRetriever().grabCategoryBlanket(cat);
        print("...Category Blanket Built.");
        pgrabber = SubjectsGrabber();
        blanketSubjects = pgrabber.grabPages(blanket, sub);
        print((str(len(blanketSubjects))) + ( " subject" if (len(blanketSubjects) is 1) else " subjects") + " found");
        blanketPages = pf.formPages("en", blanketSubjects);
        corpusBuilder = CorpusBuilder();
        corpusBuilder.Build(blanketPages, cat);
        
        #Need to make the learni
        
                
        ('''text = raw_input("Text: ");
        matchingTerms = set([]);
        print("Building N-grams...");                                        
        ngb.run(text);
        print("...N-grams built.");
        print("Processing...");
        for ngram in ngb.ngramList: 
            wikiGrab.grab(ngram);
            if(len(wikiGrab.cat) > 0):
                print(ngram);
                for cat in wikiGrab.cat:
                    if(cat in blanket):
                        print("Match found!");
                        matchingTerms.add(ngram);
                        break;
            wikiGrab.clear();
            #We may not need this part
        print("Relevant terms");
        for ngram in matchingTerms:
            print (ngram); ''');         
    elif(choice == "2"):
        #Then prepare to leave...
        print("Thank you for using the program, and hope you like the menu system....");
    else:
        # OOPS... you broke it...
        print("I am confused by what you want from me. Please type a 1 or a 2 to select a choice from the menu.");

